package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Banner;
import cn.wolfcode.wolf2w.query.BannerQuery;
import cn.wolfcode.wolf2w.repository.BannerRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IBannerService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BannerServiceImpl implements IBannerService {

    @Autowired
    private MongoTemplate template;
    @Autowired
    private BannerRepository bannerRepository;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IDestinationService destinationService;



    @Override
    public Banner get(String id) {
        return bannerRepository.findById(id).get();
    }

    @Override
    public List<Banner> selectAll() {
        return bannerRepository.findAll();
    }

    @Override
    public Page<Banner> query(BannerQuery qo) {
        //创建Query对象,根据过滤条件查询总条数,此处没有限制条件,所以用空的查询对象查
        Query query = new Query();
        //添加限制,如果有目的地
        //创建过滤查询
        //有数据,进行分页操作,创建新的分页对象,此索引从0开始,所以要减一,分页的字段与数据库一致
        //如果有分类类别
        Pageable pageAble= PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC,"_id");
        //返回给控制器
        Page<Banner> page = DBHelper.query(template, Banner.class, query, pageAble);
        return page;
    }

    @Override
    public void update(Banner banner) {
        bannerRepository.save(banner);
    }

    @Override
    public void save(Banner banner) {
        banner.setId(null);
        bannerRepository.save(banner);
    }

    @Override
    public void deleteById(String id) {
        bannerRepository.deleteById(id);
    }

    @Override
    public List<Banner> queryFive() {
        Query query = new Query();
        Pageable pageable=PageRequest.of(0, 5, Sort.Direction.ASC,"sequence");
        query.with(pageable);
        List<Banner> list = template.find(query, Banner.class);
        return list;
    }
}
